
<template>
  <el-dialog
    title="安全隐患分类信息"
    :visible.sync="show"
    center
  >
    <!-- width="1100px" -->

    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="90px">
      <el-form-item label="分类编号" prop="number">
        <el-input v-model="form.number" :disabled="disableForm" />
      </el-form-item>
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="form.name" :disabled="disableForm" />
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-checkbox v-model="form.enable" />
      </el-form-item>
      <el-form-item label="说明" prop="description">
        <el-input v-model="form.description" :disabled="disableForm" />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveClassify">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addSecurityRiskCategory, updateSecurityRiskCategory } from '@/api/securityManage'
export default {
  name: 'AddHiddenDangerClassify',
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    addLevel: {
      type: String,
      default: ''
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    relateData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      form: {
        name: '',
        number: '',
        description: '',
        enable: true
      },
      rules: {
        name: [
          { required: true, message: '请输入安全隐患名称', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        // this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            this.form[key] = this.data[key]
          })
        })
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
        this.targetList = []
      }
    }
  },
  mounted() {
  },
  methods: {
    getSelectedRows(data) {
      this.targetList = this.targetList.concat(data.map(item => {
        return {
          ...item,
          basFraction: '',
          basRemark: ''
        }
      }))
    },
    selectSupplier(data) {
      if (data) {
        this.supplierInfo = data
        const { name, number, unifiedSocialCreditCode, identifyNumber } = data
        this.form.supName = name
        this.form.supNumber = number
        this.form.supUnifiedSocialCreditCode = unifiedSocialCreditCode
        this.form.supIdentifyNumber = identifyNumber
      }
    },
    saveClassify() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const parentId = this.addLevel === 'same' ? (this.relateData ? this.relateData.parentId : null) : (this.relateData ? this.relateData.id : null)
          const params = {
            ...this.form,
            ...this.optType === 'edit' ? {
              id: this.data.id
            } : {
              parentId
            }
          }
          const fn = this.optType === 'add' ? addSecurityRiskCategory : updateSecurityRiskCategory
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              // this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>

